/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.empleados;

import com.mapping.Departamento;
import com.mapping.Empleado;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Ricardo
 */
@Namespace(value = "/")
@Action(value = "gestionModificarEmpleadoPre", results = {
    @Result(name = "success", location = "/GestionEmpleados/gestionModificarEmpleado.jsp")})
public class ModificarEmpleadoPre extends ActionSupport{
    private Empleado empleado;
    private Departamento departamento;
    private Departamento[] departamentos;
    private String idEmpleado;
    
    @Override
    public String execute(){                     
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();                     
        empleado = (Empleado)em.getReference(Empleado.class, idEmpleado);           
        departamento = empleado.getDepartamento(); 
        
        TypedQuery  e=em.createQuery("SELECT d FROM Departamento d", Departamento.class);    
        List<Departamento> resultList = e.getResultList();
        departamentos = new Departamento[resultList.size()];
                
        for(int i=0; i <resultList.size(); i++){                                             
            departamentos[i] = resultList.get(i);           
        }        
        
        em.flush();
        tx.commit();
        em.close();                 
        return SUCCESS;
    }

    public Empleado getEmpleado() {
        return empleado;
    }

    public Departamento getDepartamento() {
        return departamento;
    }

    public void setIdEmpleado(String idEmpleado) {
        this.idEmpleado = idEmpleado;
    }       

    public Departamento[] getDepartamentos() {
        return departamentos;
    }
        
}
